package algorithm.poj.p1000;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLDecoder;
import java.util.StringTokenizer;

/**
 * 分析：
 * 
 * 实现：
 * 经验：
 * 教训：
 * 分类：并查集
 * 
 * @author wong.tong@gmail.com
 *
 */
public class P1182 {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {

		InputStream input = null;
		if (false) {
			input = System.in;
		} else {
			URL url = P1182.class.getResource("P1182.txt");
			File file = new File(URLDecoder.decode(url.getPath(), "UTF-8"));
			input = new FileInputStream(file);
		}
		
		BufferedReader stdin = new BufferedReader(new InputStreamReader(input));

		String line = stdin.readLine();
		StringTokenizer st = new StringTokenizer(line);
		int N = Integer.valueOf(st.nextToken());
		Animal[] animals= new Animal[N];
		for (int i = 0; i< N; i ++) {
			animals[i] = UnionFind.make(i);
		}
		
		int K = Integer.valueOf(st.nextToken());
		int count = 0;
		for (int i = 0; i < K; i ++) {
			line = stdin.readLine();
			st = new StringTokenizer(line);
			int D = Integer.valueOf(st.nextToken());
			int X = Integer.valueOf(st.nextToken())-1;
			int Y = Integer.valueOf(st.nextToken())-1;
			if (X >= N || Y >= N || (D == 2 && X == Y)) {
				count ++;
			} else {
				Animal a = animals[X];
				Animal b = animals[Y];
				if (D == 1) {
					if (a.type != 'X' && b.type != 'X' && a.type != b.type) {
						count ++;		//FALSE
					} else {
						UnionFind.union(a, b);	//TRUE
					}
				} else {	//D == 2
					if (a.type == 'X' && b.type == 'X') {
					} else if (a.type == 'X' && b.type != 'X') {
						
					} else if (a.type != 'X' && b.type == 'X') {
						
					} else {
						
					}
				}
			}
		}
	}
	
	private static class Animal {
		Animal parent;
		char type;	//A,B,C,X("Unknown")
		int index;	//start from 0
		int rank;
	}
	
	private static class UnionFind {
		
		public static Animal make(int index) {

			Animal animal = new Animal();
			animal.type = 'X';
			animal.parent = animal;
			animal.index = index;
			animal.rank = 0;
			return animal;
		}
		
		public static void union(Animal a, Animal b) {
			link(findSet(a), findSet(b));
		}
		
		private static void link(Animal a, Animal b) {
			
			if (a.rank > b.rank) {
				b.parent = a;
			} else {
				a.parent = b;
				if (a.rank == b.rank) {
					b.rank = b.rank+1;
				}
			}
			
			if (a.type == 'X' && b.type != 'X') {
				a.type = b.type;
			} else if (a.type != 'X' && b.type == 'X') {
				b.type = a.type;
			}
		}
		
		public static Animal findSet(Animal x) {
			
			if (x != x.parent) { 
				x.parent = findSet(x.parent);
			}
			return x.parent;
		}
	}
}